/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.states;

import com.google.common.base.Preconditions;
import com.googlecode.maurersmarbles.GameState;
import com.googlecode.maurersmarbles.InputEvent;
import com.googlecode.maurersmarbles.RGB;
import com.googlecode.maurersmarbles.Rectangle;
import com.googlecode.maurersmarbles.Screen;
import com.googlecode.maurersmarbles.TouchEvent;
import com.googlecode.maurersmarbles.drawables.ButtonDrawable;
import com.googlecode.maurersmarbles.drawables.Drawable;
import com.googlecode.maurersmarbles.drawables.RectangleDrawable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author Daniel Uppenkamp
 */
public class MainMenuState implements State {

    /**
     * The MainMenuState instance.
     */
    private static final MainMenuState INSTANCE = new MainMenuState();
    /**
     * The text for the help button.
     */
    protected static final String BUTTON_TEXT_HELP = "Help";
    /**
     * The text for the new game button.
     */
    protected static final String BUTTON_TEXT_NEW_GAME = "New Game";
    /**
     * The text for the options button.
     */
    protected static final String BUTTON_TEXT_OPTIONS = "Options";
    /**
     * The text for the resume game button.
     */
    protected static final String BUTTON_TEXT_RESUME_GAME = "Resume Game";
    /**
     * The list of button drawables.
     */
    protected List<ButtonDrawable> buttonDrawables = new ArrayList();

    /**
     * Creates a new MainMenuState.
     */
    private MainMenuState() {

        buttonDrawables.add(new ButtonDrawable(BUTTON_TEXT_NEW_GAME));
        //buttonDrawables.add(new ButtonDrawable(BUTTON_TEXT_RESUME_GAME));
        buttonDrawables.add(new ButtonDrawable(BUTTON_TEXT_OPTIONS));
        buttonDrawables.add(new ButtonDrawable(BUTTON_TEXT_HELP));

    }

    /**
     * Returns the drawables to draw on the screen.
     *
     * @param screen the screen
     * @return the drawables to draw on the screen
     */
    protected Collection<Drawable> getDrawables(Screen screen) {

        Preconditions.checkNotNull(screen, "Null screen");
        
        Collection<Drawable> drawables = new ArrayList();

        drawables.add(new RectangleDrawable(0, 0, screen.getWidth(), screen.getHeight(), RGB.WHITE, true));

        // TODO Finish designing/implementing the main menu drawables
        // TODO Move these values to constants
        int padding = screen.getHeight() / 75;
        double buttonWidth = screen.getWidth() * ButtonDrawable.SCREEN_WIDTH_TO_BUTTON_WIDTH;
        double buttonHeight = screen.getHeight() * ButtonDrawable.SCREEN_HEIGHT_TO_BUTTON_HEIGHT;
        double buttonStartX = (screen.getWidth() / 2.0) - (buttonWidth / 2.0);
        double buttonStartY = (screen.getHeight() / 3) + padding;

        for (int i = 0; i < buttonDrawables.size(); i++) {
            buttonDrawables.get(i).setRectangle(new Rectangle((int) buttonStartX, (int) (buttonStartY + ((buttonHeight + padding) * i)), (int) buttonWidth, (int) buttonHeight));
            drawables.add(buttonDrawables.get(i));
        }

        return drawables;

    }

    /**
     * Returns the MainMenuState instance.
     *
     * @return the MainMenuState instance
     */
    public static MainMenuState getInstance() {
        return INSTANCE;
    }

    @Override
    public State enter(Screen screen, GameState gameState) {

        // check to see if we have a screen
        if (screen != null) {

            // get the drawables and draw them on the screen
            screen.draw(getDrawables(screen));

        }

        // return null to stay in this state
        return null;

    }

    @Override
    public State handleInputEvent(Screen screen, GameState gameState, InputEvent inputEvent) {

        Preconditions.checkNotNull(inputEvent, "Null input event");
        Preconditions.checkNotNull(buttonDrawables, "Null button drawables");
        
        State newState = null;

        // check to see if we have a screen
        if (screen != null) {

            // reset the buttons to not pressed
            for (ButtonDrawable buttonDrawable : buttonDrawables) {
                buttonDrawable.setPressed(false);
            }

            if (inputEvent instanceof TouchEvent) {

                TouchEvent touchEvent = (TouchEvent) inputEvent;

                for (ButtonDrawable buttonDrawable : buttonDrawables) {

                    Rectangle rectangle = buttonDrawable.getRectangle();
                    if (rectangle != null && rectangle.contains(touchEvent.getX(), touchEvent.getY())) {

                        if (touchEvent.getType() == TouchEvent.TYPE_PRESSED) {
                            buttonDrawable.setPressed(true);
                        }
                        else if (touchEvent.getType() == TouchEvent.TYPE_RELEASED) {

                            switch (buttonDrawable.getText()) {

                                case BUTTON_TEXT_NEW_GAME:
                                    newState = NewGameState.getInstance();
                                    break;

                                case BUTTON_TEXT_OPTIONS:
                                    newState = OptionsState.getInstance();
                                    break;

                                case BUTTON_TEXT_HELP:
                                    newState = HelpState.getInstance();
                                    break;

                            }

                        }

                        break;

                    }

                }

                screen.draw(getDrawables(screen));

            }

        }

        return newState;

    }

    @Override
    public void handleResizeEvent(Screen screen, GameState gameState, int width, int height) {

        // check to see if we have a screen
        if (screen != null) {

            // get the drawables and draw them on the screen
            screen.draw(getDrawables(screen));

        }

    }

}
